<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2024/9/18
  Time: 上午9:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>申请借用设备</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <!-- 设备编号 -->
    <div class="layui-form-item">
        <label class="layui-form-label">设备编号</label>
        <div class="layui-input-block">
            <input type="text" id="id" name="id" lay-verify="required" placeholder="" value="" class="layui-input" readonly>
        </div>
    </div>

    <!-- 设备名称 -->
    <div class="layui-form-item">
        <label class="layui-form-label ">设备名称</label>
        <div class="layui-input-block">
            <input type="text" id="name" name="name" lay-verify="required" placeholder="" value="" class="layui-input" readonly>
        </div>
    </div>

    <!-- 设备类型 -->
    <div class="layui-form-item">
        <label class="layui-form-label ">设备类型</label>
        <div class="layui-input-block">
            <input type="text" id="type" name="type" placeholder="" value="" class="layui-input" readonly>
        </div>
    </div>

    <!-- 所属教师 -->
    <div class="layui-form-item">
        <label class="layui-form-label ">所属教师</label>
        <div class="layui-input-block">
            <input type="text" id="staff_id" name="staff_id" placeholder="" value="" class="layui-input" readonly>
        </div>
    </div>

    <!-- 借用天数 -->
    <div class="layui-form-item">
        <label class="layui-form-label">借用天数</label>
        <div class="layui-input-block">
            <input type="number" id="expected_days" name="expected_days" lay-verify="required" placeholder="请输入借用天数" class="layui-input">
        </div>
    </div>

    <!-- 备注信息 -->
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea id="remarks" name="remarks" class="layui-textarea" placeholder="" ></textarea>
        </div>
    </div>


    <!-- 提交按钮 -->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submitBorrowApplication">提交借用申请</button>
        </div>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'jquery'], function () {
        var form = layui.form,
            $ = layui.jquery;

        // 获取设备ID
        var equipmentId = new URLSearchParams(window.location.search).get('id');
        if (equipmentId) {
            $.ajax({
                url: '${pageContext.request.contextPath}/equipmentServlet',
                type: 'GET',
                data: { method: 'getEquipmentById', id: equipmentId },
                success: function (response) {
                    var data = JSON.parse(response);
                    if (data.code === 0) {
                        var equipment = data.data;
                        // 填充表单
                        $('#id').val(equipment.id);
                        $('#name').val(equipment.name);
                        $('#type').val(equipment.type);
                        $('#staff_id').val(equipment.staff_id);
                    } else {
                        alert("设备详情获取失败");
                    }
                },
                error: function () {
                    alert("请求失败");
                }
            });
        } else {
            alert("未获取到设备ID");
        }

        // 提交借用申请
        form.on('submit(submitBorrowApplication)', function (data) {
            var borrowData = {
                equipment_id: $('#id').val(),
                name: $('#name').val(),
                type: $('#type').val(),
                staff_id: $('#staff_id').val(),
                expected_days: $('#expected_days').val(),
                remarks: $('#remarks').val()
            };

            $.ajax({
                url: '${pageContext.request.contextPath}/borrowServlet',
                type: 'POST',
                data: {
                    method: 'addBorrowApplication',
                    data: JSON.stringify(borrowData)
                },
                success: function (response) {
                    if (response == 1) {
                        layer.msg('借用申请提交成功');
                    } else {
                        layer.msg('借用申请提交失败');
                    }
                },
                error: function () {
                    layer.msg('请求失败，请稍后重试');
                }
            });

            return false;  // 阻止表单默认提交
        });
    });
</script>
</body>
</html>